/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.databasemanager.configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import database.databasemanager.exceptions.DataBaseException;
import database.databasemanager.exceptions.InvalidDataBaseAccessInfoException;

/**
 *
 * @author Vicky
 */
public class DataBaseInfoLoader {

   private String dataBaseUserName;
   private String dataBaseUserPassword;
   private String dataBaseServerIP; 
   private String dataBaseServerInputPort;
   private String dataBaseName;


   

   public void loadDataBaseAccessInfo() throws DataBaseException{
        InputStream inputStream  = null;
        Properties dataBaseAccessInfo = new Properties();
        try {
            inputStream = new FileInputStream(DataBaseAccessInfoLabels.ACCES_INFO_FILE_NAME);
            dataBaseAccessInfo.load(inputStream);
            dataBaseServerIP=dataBaseAccessInfo.getProperty(DataBaseAccessInfoLabels.SERVER_IP);
            dataBaseUserName= dataBaseAccessInfo.getProperty(DataBaseAccessInfoLabels.USER_NAME);
            dataBaseUserPassword=dataBaseAccessInfo.getProperty(DataBaseAccessInfoLabels.USER_PASSWORD);
            dataBaseServerInputPort=dataBaseAccessInfo.getProperty(DataBaseAccessInfoLabels.SERVER_INPUT_PORT);
            dataBaseName=dataBaseAccessInfo.getProperty(DataBaseAccessInfoLabels.DATA_BASE_NAME);
        }catch (FileNotFoundException exception) {
            throw new InvalidDataBaseAccessInfoException(exception);
        } catch (IOException exception) {
            throw new InvalidDataBaseAccessInfoException(exception);
        } finally {
            try {
                inputStream.close();
            } catch (IOException exception) {
                 throw new InvalidDataBaseAccessInfoException(exception);
            }
        }

   }

   public String getDataBaseUserName() {
        return dataBaseUserName;
    }

   public String getDataBaseUserPassword() {
        return dataBaseUserPassword;
    }

   public String getDataBaseAddress(){
       String dataBaseAddress= DataBaseAccessInfoLabels.MYSQL_ROOT + dataBaseServerIP+
                                    DataBaseAccessInfoLabels.COLON+
                                    dataBaseServerInputPort+
                                    DataBaseAccessInfoLabels.SLASH+
                                    dataBaseName;
       return dataBaseAddress;
   }
}
